/*************************************************************************
> File Name: 01_test.cpp
> Author: hzy
> Created Time: 2023-08-15 11:24:12
> Description:函数模板
************************************************************************/
#include <iostream>

using namespace std;

#if 0
int Swap(int &a, int &b)
{
    cout << "int" << endl;
    int temp = a;
    a = b;
    b = temp;
}

float Swap(float &a, float &b)
{
    cout << "float" << endl;
    float temp = a;
    a = b;
    b = temp;
}

char Swap(char &a, char &b)
{
    cout << "char" << endl;
    char temp = a;
    a = b;
    b = temp;
}
#else
template <class T>
void Swap(T &a, T &b)
{
    cout << "template" << endl;
    T temp = a;
    a = b;
    b = temp;
}

// 不同类型的参数
template <class T, class U>
void Swap(T &a, U &b)
{
    cout << "template with different types" << endl;
    T temp = a;
    a = b;
    b = temp;
}

#endif

void test_01()
{
    int a = 97;
    int b = 9;

    float c = 6.5;
    float d = 5.6;

    char e = 'p';
    char f = 'q';

    cout << "a = " << a << ", e = " << e << endl;
    Swap(a, e); // 调用不同类型参数的模板
    cout << "a = " << a << ", e = " << e << endl;
    cout << "==========" << endl;

    cout << "c = " << c << ", d = " << d << endl;
    Swap<>(c, d);
    cout << "c = " << c << ", d = " << d << endl;
    cout << "==========" << endl;

    cout << "e = " << e << ", f = " << f << endl;
    Swap<char>(e, f); // 显示指定类型为char
    cout << "e = " << e << ", f = " << f << endl;
    cout << "==========" << endl;
}


int main()
{
    test_01();

    return 0;
}